<?php

if(Antz_IntelliForm::submitted('lost_pass')){
    $Antz = Antz::registry('Antz');
    $username = preg_replace('/[^a-zA-Z0-9\-_]/', '', post('username'));
    $email = post('email');
    $error = false;
    if($email != '' && !Antz_Filter::checkEmail($email)){
        $error = true;
        Antz::addFormError('email', $this->Lang('invalid_format'));
    }else{
        $emailMd5 = md5(strtolower($email));
    };
    $found = false;
    if($DB->countRows(cms_db_prefix().'iusers', "username = '{$username}'")>0){
        $found = true;
        $userid = $DB->oneValue(cms_db_prefix().'iusers', 'id', "username = '{$username}'");
    };
    if($DB->countRows(cms_db_prefix().'iusers', "email_md5 = '{$emailMd5}'")>0){
        $found = true;
        $userid = $DB->oneValue(cms_db_prefix().'iusers', 'id', "email_md5 = '{$emailMd5}'");
    };

    if($found){
        $uData = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}iusers WHERE id = '{$userid}'");

        $emailParams = array(
            'username'=>$uData['username'],
            'email'=>$CRYPT->decrypt($uData['email_crypt']),
            'password'=>$CRYPT->decrypt($uData['pass']),
            'login_url'=>$Antz->GetUrlById($this->GetConfig('login_id'))
        );
        foreach($emailParams as $k=>$v){
            $this->smarty->assign('IUsers_'.$k, $v);
        };
        $msg = $this->ProcessEmailTemplate('lost_password.php', $emailParams);

        $subject = 'Password reminder';


        $toFrom = array(
            'toName' => $uData['first_name'].' '.$uData['last_name'],
            'toEmail' => $CRYPT->decrypt($uData['email_crypt'])
        );

        Antz_Email::sendRaw($subject, $msg, $toFrom, true);

        $domain = explode('@', $CRYPT->decrypt($uData['email_crypt']));
        $domain = $domain[1];

        Antz::addSuccess($this->Lang('password_sent_to', $domain));

        $hookParams = array('user_data' => $uData);
		$newParams = Antz::Hookup('IUsers_LostPasswordProcess', $hookParams);
		unset($newParams);
		
		Antz::goLastPage();

    }

}

$formFields = array(
	array(
		'type' => 'text',
		'name' => 'username',
		'label' => $this->Lang('username'),
		'value' => post('username')
	),
	array(
		'type' => 'text',
		'name' => 'email',
		'label' => $this->Lang('email'),
		'value' => post('email')
	)
);

// ready to create our form

if($this->GetConfig('auto_forms')){

	// prepare the structure for auto form generation
	$blocks = array(
		0 => array(
			'type' => 'full',
			'fieldsets' => array(
				0 => array(
					'legend' => $this->Lang('lost_password_legend'),
					'side' => 'left',
					'fields' => $formFields
				)
			)
		)
	);

	$form = Antz::makeForm($blocks, 'lost_pass');

	$form->addSubmit($this->Lang('send_reminder'));

	echo $form->render();

}else{
	// assigning form components to smarty variables and processing template
	if($this->GetConfig('wrap_form_components')) $wrap = true;
	else $wrap = false;
	if($this->GetConfig('show_labels')) $labels = true;
	else $labels = false;

	foreach($formFields as $field){
		$component = Antz::makeFormComponent($field, $wrap, $labels);
		$this->smarty->assign('fc_'.$field['name'], $component->render());
	};

	$this->smarty->assign('lost_password_intelliform_seed', Antz_IntelliForm::seed('lost_pass'));

	echo $this->ProcessTemplate('lost_password.tpl');

}


/*
$form = Antz::newObject('Antz_Form', 'lost_pass');
$block = $form->add('full');
$fs = $block->add($this->Lang('lost_password_retrieval'));

$fs->addInput('text', 'username', $this->Lang('username'), post('username'));
$fs->addInput('text', 'email', $this->Lang('email'), post('email'));
$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('send_reminder'));

echo $form->render();
*/